<template>
  <div class="app-container">
    <SearchForm
      v-show="showSearch"
      v-model="queryParams"
      :options="options.formOps"
      @onQuery="getList"
    >
      <template #default="{ filterForm, handleQuery }"> </template>
    </SearchForm>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <ImportExcel
          type="primary"
          :url="APIS.url"
          plain
          icon="Plus"
          @success="getList"
        ></ImportExcel>
      </el-col>
      <el-col :span="1.5">
        <ExportExcel type="primary" :url="APIS.url" plain></ExportExcel>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <Table
      ref="table"
      :auto-featch="true"
      :fetch-data="fetchData"
      :options="options.table"
      actionWidth="250"
    >
      <template #default>
        <el-table-column
          label="创建时间"
          align="center"
          key="createTime"
          prop="createTime"
          width="200"
        />
        <el-table-column
          label="更新时间"
          align="center"
          key="updateTime"
          prop="updateTime"
          width="200"
        />
      </template>
      <template #actionButton="{ row }">
        <el-button type="text" icon="Edit" @click="handleEdit(row)"
          >修改</el-button
        >
        <el-button type="text" icon="Edit" @click="handleCheck(row, 0)"
          >查看</el-button
        >
        <el-button type="text" icon="Edit" @click="handleCheck(row, 1)"
          >回放</el-button
        >
        <el-button
          type="text"
          icon="Delete"
          style="color: red"
          @click="handleDel(row)"
          >删除</el-button
        >
      </template></Table
    >
    <DialogVform
      :formJson="formJson"
      ref="dialogVform"
      @onSubmit="onSubmit"
      :title="dialogType == 'add' ? '新增监控信息' : '修改监控信息'"
    >
    </DialogVform>
  </div>
</template>

<script setup>
import Table from "@/components/Table/index.vue";
import SearchForm from "@/components/searchForm/index";
import DialogVform from "@/components/dialogVform/index";
import { apis as APIS } from "@/api/hdwarning/spjkxx/index.js";
import formJson from "./components/options.json";
import { useAsyncData, useFormJson } from "@/views/hooks/useAsyncData.js";
import ImportExcel from "@/components/ImportExcel/index.vue";
import ExportExcel from "@/components/ExportExcel/index.vue";
import { useRouter } from "vue-router";
const router = useRouter();
const ops = useFormJson(formJson, {
  sort: [],
  sortable: [],
});
const { proxy } = getCurrentInstance();
const showSearch = ref(true);
const handleCheck = async (row, type) => {
  if (row.mapCode && type == 0) {
    try {
      const res = await APIS.getUrl({
        mapCode: row.mapCode,
      });
      let routeUrl = router.resolve({
        path: "/jkNew",
        query: { videoUrl: res },
      });
      window.open(routeUrl.href, "_blank");
    } catch (error) {
      console.log(111);
      let routeUrl = router.resolve({
        path: type === 0 ? "/jk" : "/jkback",
        query: { ...row, type },
      });
      window.open(routeUrl.href, "_blank");
    }

    return;
  }
  let routeUrl = router.resolve({
    path: type === 0 ? "/jk" : "/jkback",
    query: { ...row, type },
  });
  window.open(routeUrl.href, "_blank");
};
const options = reactive({
  table: ops,
  formOps: ops.filter((i) =>
    ["monitorName", "addressInfo", "monitorCode"].includes(i.prop)
  ),
});
const form = ref({});
const table = ref();
const dialogVform = ref();
const {
  onSubmit,
  fetchData,
  dialogType,
  handleAdd,
  handleEdit,
  getList,
  handleDel,
  queryParams,
  handleExport,
} = useAsyncData({
  APIS,
  formJson,
  dialogVform,
  table,
  proxy,
  form: form.value,
});
</script>

<style lang="scss" scoped></style>
